exports.communicationBrowse = (req, res) => {

    var restfulParams = req.routeInfo.restfulParams;

    var resultData = {};

    var todayStart = new Date(new Date().toLocaleDateString()).getTime() / 1000;

    var todayEnd = (new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000) / 1000;

    var todayOrTotal = restfulParams.todayOrTotal;//0-今日 1-所有

    var skip = restfulParams.skip == undefined ? 0 : restfulParams.skip;

    var limit = restfulParams.limit == undefined ? 20 : restfulParams.limit;

    var u_id = restfulParams.u_id;

    var u_is_manager = restfulParams.u_is_manager;

    var u_type = restfulParams.u_type;
    // 个人 查看个人
    var condition = "/apurr_uid/" + u_id + "/between/" + todayStart + "-apurr_create_time-" + todayEnd;

    if(u_type < 1 && u_is_manager > 0){
        //超管 查看个人
        condition = "/between/" + todayStart + "-apurr_create_time-" + todayEnd;
        if (todayOrTotal == 1) {
            //超管 查看所有
            condition = ''
        }
    }else{
        if (todayOrTotal == 1) {
            // 个人 查看所有
            condition = "/apurr_uid/" + u_id
        }
    }

    //浏览人员列表
    function readingShareMemberList(cb) {

        R.SEND_HTTP(
            req,
            res,
            {
                url: config.external_server.apiAddress + "/organization/search_art_product_user_read_record/" + condition + "/sort_order/apurr_id-desc/" + "/skip/" + skip + "/limit/" + limit,
                method: 'GET',
            },
            function (data) {

                var readingArr = [];

                if (data.result.data.length > 0) {
                    for (var i = 0; i < data.result.data.length; i++) {
                        if (data.result.data[i].apuur_mcid > 0) {
                            readingArr.push(data.result.data[i])
                        }
                    }
                }

                resultData.browseMember = readingArr;

                cb();

            }
        )
    }

    //查询每个浏览者的身份
    function queryEveryCustomerPosition(cb) {

        F.SerialEach(resultData.browseMember, function (item, index, callback) {

            //查询客户信息
            function customerData(eachCb) {

                R.SEND_HTTP(
                    req,
                    res,
                    {
                        url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/c_is_delete/0/c_openid/" + item.apurr_openid,
                        method: 'POST',
                        data: {
                            fields: ['c_id', 'c_uid', 'c_follow_status', 'c_is_buy'].join(",")
                        }
                    },
                    function (data) {

                        var customerInfo = data.result.data;
                        var c_id = 0;
                        var customerPosition = 0;
                        var isCustomer = false;
                        var isMyCustomer = false;
                        var isMyShare = false;
                        if (customerInfo.length > 0) {
                            c_id = data.result.data[0].c_id;
                            isCustomer = true;
                            if (restfulParams.u_id == customerInfo[0].c_uid) {
                                isMyCustomer = true;
                            }

                            if (isCustomer) {
                                if (isMyCustomer) {
                                    customerPosition = 1
                                } else {
                                    customerPosition = 2
                                }
                            }

                        }
                        if (item.apurr_uid == restfulParams.u_id) {
                            isMyShare = true
                        }

                        item.isCustomer = isCustomer;
                        item.c_id = c_id;
                        item.isMyCustomer = isMyCustomer;
                        item.customerPosition = customerPosition;
                        item.isMyShare = isMyShare;
                        eachCb();

                    }
                )
            }


            F.Parallel([
                customerData,
            ], () => {
                callback();
            })

        }, cb)

    }

    F.Serial([
        readingShareMemberList,
        queryEveryCustomerPosition,
    ], () => {
        RES.successResponse(res, resultData);
    })


}